﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace EpiMapit
{
	public class BoundingBox
	{
		public float North;
		public float South;
		public float East;
		public float West;

		public BoundingBox(float north, float south, float east, float west)
		{
			this.North = north;
			this.South = south;
			this.East = east;
			this.West = west;
		}

		public BoundingBox(IEnumerable<IKmlFigure> figures)
			: this(figures.Select(x => x.BoundingBox))
		{
		}

		public BoundingBox(IEnumerable<BoundingBox> boxes)
		{
			North = float.MinValue;
			South = float.MaxValue;
			East = float.MinValue;
			West = float.MaxValue;
			
			foreach (var square in boxes)
			{
				South = Math.Min(South, square.South);
				North = Math.Max(North, square.North);
				West = Math.Min(West, square.West);
				East = Math.Max(East, square.East);
			}	
		}

        public XElement Kml
        {
            get
            {
                return 
                    new XElement("LatLonAltBox",
                        new XElement("north", North),
                        new XElement("south", South),
                        new XElement("east", East),
                        new XElement("west", West)
                    );
            }
        }
	}
}
